package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.c.entity.Order;
import com.c.entity.Setmeal;
import com.c.service.OrderService;
import com.c.service.SetmealService;
import com.c.service.UserService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:23
 */
@RestController
@RequestMapping("/orderlist")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查找订单
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {

        return orderService.findByPage(queryPageBean);
    }

    /**
     * 添加预约订单
     *
     * @param map
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Map map) {
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            Result result = orderService.order(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDERLIST_FAIL);
        }
    }

    /**
     * 用于弹出编辑框 ,返回对应id的订单 和 所有的体检套餐
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING_QUERY')")
    @RequestMapping("/findByIdWithAllSetmeal")
    public Result findByIdWithAllSetmeal(Integer id) {
        try {
            Map map = orderService.findById(id);
            List<Setmeal> setmeals = setmealService.list();

            Map data = new HashMap();
            data.put("map", map);
            data.put("setmeals", setmeals);

            return new Result(true, MessageConstant.QUERY_ORDERLIST_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDERLIST_FAIL);
        }
    }

    /**
     * 编辑预约订单
     *
     * @param order
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order) {
        return orderService.edit(order);
    }

    /**
     * 删除预约订单
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ORDERLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDERLIST_FAIL);
        }
    }
}
